package com.xmx.confu_work.useAdmin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmx.confu_work.common.vo.Result;
import com.xmx.confu_work.useAdmin.entity.User;
import com.xmx.confu_work.useAdmin.service.IUserService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dinzhen
 * @since 2024-04-22
 */
@RestController
@RequestMapping("/useAdmin/user")
public class UserController {
    @Resource
    private IUserService iUserService;

    @Resource
    private PasswordEncoder passwordEncoder;

    @GetMapping("/getuser")
    public Result<List<User>> getUser(){
        List<User> userList = iUserService.list();
        return Result.success(userList,"查询成功");
    }

    //登录
    @PostMapping("/login")
    public Result<Map<String,Object>> login(@RequestBody User user){
        //随着新的加密来了。老的逻辑这下用不了了
        Map<String,Object> data = iUserService.login(user);
        if (data != null){
            return Result.success(data);
        }
            return Result.faid(20002,"用户名和密码错误");
    }

    @GetMapping("/info")
    public Result<Map<String,Object>> getUserInfo(@RequestParam("token")String token){
        Map<String,Object> data = iUserService.getUserInfo(token);
        if (data != null){
            return Result.success(data);
        }
        return Result.faid(20003,"登录信息无效，重新登录");
    }

    @PostMapping("/logout")
    public Result<?> logout(@RequestHeader("x-Token")String token){
        iUserService.logout(token);
        return Result.success();
    }


    @GetMapping("/list")
    public Result<Map<String,Object>> getUserList(@RequestParam(value = "username",required = false)String username,@RequestParam(value = "phone",required = false) String phone,@RequestParam("pageNo") Long pageNo,@RequestParam("pageSize") Long pageSize){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasLength(username),User::getUsername,username);
        wrapper.eq(StringUtils.hasLength(phone),User::getPhone,phone);
        //我寻思这些东西不能丢在这啊
        Page<User> pages = new Page<>(pageNo,pageSize);
        iUserService.page(pages,wrapper);

        Map<String,Object> data = new HashMap<>();
        data.put("total",pages.getTotal());
        data.put("rows",pages.getRecords());

        return Result.success(data);

    }

    @PostMapping("/add")
    public Result<?> addUser(@RequestBody User user){
        //加密
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        iUserService.save(user);
        return Result.success("新增成功");
    }

    @PutMapping("updata")
    public Result<?> updataIser(@RequestBody User user){
        user.setPassword(null);
        iUserService.updateById(user);
        return Result.success("修改数据成功");
    }

    @GetMapping("/getId/{id}")
    public Result<User> getUserById(@PathVariable("id") Integer id){
        User user = iUserService.getById(id);
        return Result.success(user);
    }

    @DeleteMapping("/delete/{id}")
    public Result<User> deleteUserById(@PathVariable("id") Integer id){
        iUserService.removeById(id);
        return Result.success("删除成功");
    }
}
